﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace SAM_Edu.dao
{
    public class daoFeatures
    {

        public static ulong getUserRights(string username)
        {
            ulong value = 0x0000000000000000;
            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "Select * FROM accessrights WHERE Username='" + username + "'";
            MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader2;
            conn.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            while (MyReader2.Read())
            {

                value = Convert.ToUInt64(MyReader2["Value"]);


            }
            conn.Close();
            return value;
        }


        static string errMsg;
        public static DataSet loadFeatureList()
        {
            MySqlConnection conn = DatabaseConnection.getConnection();

            MySqlDataAdapter da;
            DataSet features = new DataSet();

            string cmd = "SELECT * FROM Configurator";

            try
            {
                conn.Open();
                da = new MySqlDataAdapter(cmd, conn);
                da.Fill(features);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return features;
        }
        private static bool checkExist(string username)
        {
            bool exist = false;
            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "Select * FROM accessrights WHERE Username='" + username + "'";
            MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader2;
            conn.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            if (MyReader2.Read())
            {
                exist = true;
            }
            conn.Close();
            return exist;
        }
        public static bool UpdateUserRights(string username, ulong value)
        {
            if (checkExist(username) == true)
            {
                MySqlConnection conn = DatabaseConnection.getConnection();
                string Query = "UPDATE accessrights SET value ='" + value + "' WHERE username = '" + username + "'";
                MySqlCommand cmd = new MySqlCommand(Query, conn);
                conn.Open();
                int result = cmd.ExecuteNonQuery();

                if (result > 0)
                {

                    return true;
                }
                else
                {
                    return false;
                }
                conn.Close();
            }
            else
            {
                MySqlConnection conn = DatabaseConnection.getConnection();
              //  string Query2 = "Delete FROM accessrights WHERE username ='" + username + "'";

                string Query = "insert into accessrights(Username,Value) values('" + username + "','" + value + "');";
             //   MySqlCommand cmd2 = new MySqlCommand(Query2, conn);
                MySqlCommand cmd = new MySqlCommand(Query, conn);
                conn.Open();
              //  cmd2.ExecuteNonQuery();
                int result = cmd.ExecuteNonQuery();

                if (result > 0)
                {

                    return true;
                }
                else
                {
                    return false;
                }
                conn.Close();
            }
            return true;
        }
    }
}
